﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using XnaCanvas;
using matrix = Microsoft.Xna.Framework.Matrix;
using color = Microsoft.Xna.Framework.Color;

namespace Test.Scenes {
    public partial class XnaBasicObj3 : UserControl {
        public XnaBasicObj3() {
            InitializeComponent();
            this.Loaded += XnaBasicObj3_Loaded;

            //this.xcanvas.Trigger.FrameRefresh += Trigger_FrameRefresh;
        }
        XObj3 xo1, xo2, xo3, xo4;
        void XnaBasicObj3_Loaded(object sender, RoutedEventArgs e) {
            var xs = this.xcanvas.Children[xcanvas.Trigger.Csn];
            xs.diffuseColor = color.White;
            xs.RefreshState += xs_CheckState;
            //xcanvas.Trigger.Rs.FillMode = Microsoft.Xna.Framework.Graphics.FillMode.WireFrame;
            //xcanvas.Trigger.Rs.CullMode = Microsoft.Xna.Framework.Graphics.CullMode.None;
            xo1 = new XObj3();
            xo2 = new XObj3();
            xo3 = new XObj3();
            xo4 = new XObj3();
            xs.Add("aaa", xo1);
            xs.Add("bb", xo2);
            xs.Add("dd", xo4);
            xs.Add("cc", xo3);
            xo1.SetColor(0, 0, 100, 200);
            xo2.SetColor(0, 100, 0, 200);
            xo3.SetColor(0, 100, 0, 200);
            xo4.SetColor(120, 0, 0, 200);
            xo1.LoadCubePrimitive(1);
            xo4.LoadTorusPrimitive();
            xo2.LoadCylinderPrimitive();
            xo3.LoadSpherePrimitive();
            var offset = 2f;
            xo4.World = matrix.CreateTranslation(-offset, -offset, 0);
            xo1.World = matrix.CreateTranslation(offset, offset, 0);
            xo2.World = matrix.CreateTranslation(offset, -offset, 0);
            xo3.World = matrix.CreateTranslation(-offset, offset, 0);
            //xo.effect.Alpha = 1.0f;
            //xo1.effect.AmbientLightColor = new Vector3(100, 100, 100);
            //xo2.effect.AmbientLightColor = new Vector3(100, 100, 100);
            //xo3.effect.AmbientLightColor = new Vector3(100, 100, 100);
            //xo4.effect.AmbientLightColor = new Vector3(100, 100, 100);
            xo1.effect.LightingEnabled = true;
            this.xcanvas.Begin();
        }

        void Trigger_FrameRefresh(XTrigger sender) {
            //在这里 写 xo的 旋转
        }

        void xs_CheckState(object sender, RefreshStateArgs e) {
            foreach (var item in e.result) {
                Quaternion q;Vector3 translate,s;
                item.Value.World.Decompose(out s, out q, out translate);
                var x = Quaternion.CreateFromYawPitchRoll(.05f, .05f, .05f);
                var x2 = q * x;
                item.Value.World = matrix.CreateScale(s) * matrix.CreateFromQuaternion(x2) * matrix.CreateTranslation(translate.X, translate.Y, translate.Z);

                //item.Value.World *= matrix.CreateFromQuaternion(new Quaternion(item.Value.World.Translation.X, item.Value.World.Translation.Y, item.Value.World.Translation.Z, .1f));
            }
        }
    }
}
